package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.DBConnection;

public class MessageInsert extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public MessageInsert() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
    
		String validateC = (String) request.getSession().getAttribute("validateCode");  
		String name  = (String)request.getParameter("mess[username]"); ;
		String email= (String)request.getParameter("mess[email]");
		String phone= (String)request.getParameter("mess[tele]");
		String mess= (String)request.getParameter("mess[message]");

if(validateC==null)
{
	response.getWriter().write("{result:'ERROR', errmsg:'verycode error'}");
	return;
}
String veryCode =request.getParameter("mess[rand_rs]");

if(veryCode==null||!validateC.equals(veryCode)){ 
	response.getWriter().write("{result:'ERROR', errmsg:'验证码错误'}");
	return;
}



if(name==null || email==""||phone==null||mess=="")
{
	response.getWriter().write("{result:'ERROR', errmsg:'请正确填写信息'}");
	return;
}
DBConnection loginData = null;
Connection conn = null;
PreparedStatement pstmt_copartner = null;
try {
	loginData =  new DBConnection();
	conn = loginData.getConn();
	String insert="insert into messages(time,name,email,phone,message) values (?,?,?,?,?)";
	pstmt_copartner=conn.prepareStatement(insert);
	pstmt_copartner.setString(1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	pstmt_copartner.setString(2,name);
	pstmt_copartner.setString(3,email);
	pstmt_copartner.setString(4,phone);
	pstmt_copartner.setString(5,mess);
	pstmt_copartner.execute();
}catch (Exception e) {
	e.printStackTrace();
	response.getWriter().write("{result:'ERROR', errmsg:'提交失败'}");
//	response.sendRedirect("/index.jsp");
	return;
}finally{
	loginData = null;
	DBConnection.closeConnection(conn);
	DBConnection.closePreparedStatement(pstmt_copartner);
}
response.getWriter().write("{result:'OK'}");

return;
}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
